import { login, logout, userinfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
    token: getToken(),
    userinfo: [],
    money: [],
    store: {
        current_id: 0,
        lists : []
    },
    roles: [],
    csrf_token: null
}

const mutations = {
    set_token: ( state, token ) => {
        state.token = token
    },
    set_userinfo: ( state, userinfo ) => {
        state.userinfo = userinfo
    },
    set_money: ( state, money ) => {
        state.money = money
    },
    set_store: ( state, { current_id, lists } ) => {
        state.store.current_id = current_id;
        state.store.lists = lists;
    },
    set_roles: ( state, roles ) => {
        state.roles = roles
    },
    set_csrf_token: ( state, token ) => {
        state.csrf_token = token
    },
}

const actions = {
    // user login
    login( { commit }, userinfo )
    {
        const { phone, password } = userinfo
        return new Promise( ( resolve, reject ) => {
            login( { phone: phone.trim(), password: password } )
                .then( response => {
                    const { result } = response;
                    if( result.status == 1 )
                    {
                        //bearer token
                        commit( 'set_token', result.auth_token );
                        setToken( result.auth_token );

                        //csrf token
                        commit( 'set_csrf_token', result.csrf_token );

                        resolve( result )
                    }else{
                        reject( result.message )
                    }
                } )
                .catch( error => {
                    reject( error )
                } )
        } )
    },

    // get userinfo
    userinfo( { commit, state } )
    {
        return new Promise( ( resolve, reject ) => {
            userinfo()
                .then( response => {
                    const { result } = response
                    if( result.status == 1 )
                    {
                        let { data } = result;
                        const { userinfo, money, store, current_store_id, permission } = data;
                        commit( 'set_userinfo', userinfo );
                        commit( 'set_money', money );
                        commit( 'set_store', { 'current_id' : current_store_id, 'lists' : store } );
                        commit( 'set_roles', permission );
                        commit( 'set_csrf_token', response.csrf_token );
                        resolve( { roles : permission } )
                    }else{
                        reject( result.message );
                    }
                } )
                .catch( error => {
                    reject( error )
                } )
        } )
    },

    // user logout
    logout( { commit, state, dispatch } )
    {
        return new Promise( ( resolve, reject ) => {
            logout( state.token )
                .then( () => {
                    commit( 'set_token', '' );
                    commit( 'set_roles', [] );
                    commit( 'userinfo', [] );
                    commit( 'money', [] );
                    removeToken()
                    resetRouter()

                    // reset visited views and cached views
                    // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
                    dispatch( 'tagsView/delAllViews', null, { root: true } )

                    resolve()
                } )
                .catch( error => {
                    reject( error )
                } )
        } )
    },

    // remove token
    resetToken( { commit } )
    {
        return new Promise( resolve => {
            commit( 'set_token', '' )
            commit( 'set_roles', [] )
            commit( 'set_csrf_token', null )
            removeToken()
            resolve()
        } )
    },

    // dynamically modify permissions
    changeRoles( { commit, dispatch }, role )
    {
        return new Promise( async resolve => {
            const token = role + '-token'

            commit( 'set_token', token )
            setToken( token )

            const { roles } = await dispatch( 'userinfo' )

            resetRouter()

            // generate accessible routes map based on roles
            const accessRoutes = await dispatch( 'permission/generateRoutes', roles, { root: true } )

            // dynamically add accessible routes
            router.addRoutes( accessRoutes )

            // reset visited views and cached views
            dispatch( 'tagsView/delAllViews', null, { root: true } )

            resolve()
        } )
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
